﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace Kooboo.Web.Areas.Commerce.Binders
{
    using Kooboo.Commerce;
    using Kooboo.IoC;

    public class EntityBinder : DefaultModelBinder
    {
        protected virtual string[] SkipProperties 
        {
            get
            {
                return new string[] { };
            }
        }

        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            //try
            //{
         
                var model = ObjectContainer.CreateInstance(modelType);
                if (model == null)
                {
                    return base.CreateModel(controllerContext, bindingContext, modelType);
                }
                else
                {
                    return model;
                }
            //}
            //catch
            //{
            //    return base.CreateModel(controllerContext, bindingContext, modelType);
            //}
        }


        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            if (SkipProperties.Contains(propertyDescriptor.DisplayName) == false)
            {
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }

        }

        protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
        {
            if (SkipProperties.Contains(propertyDescriptor.DisplayName) == false)
            {
                base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
            }
        }
    }
}